package com.myway.controller;

import com.myway.dto.common.JsonData;
import com.myway.dto.param.TravelOrderParam;
import com.myway.dto.param.TravelParam;
import com.myway.pojo.TabTravelDate;
import com.myway.pojo.TabTravelOrder;
import com.myway.pojo.TabTravelWithBLOBs;
import com.myway.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/en/travel")
public class TravelControllerEn {
    @Autowired
    private TravelService travelService;

    @RequestMapping("/loads")
    public String selectHotelCity(TravelParam param, Model model){
        model.addAttribute("param",param);
        return "en/travel_sel";
    }

    /**
     * 获取热门线路集合
     * @return
     */
    @RequestMapping("/hotload")
    @ResponseBody
    public JsonData selectHotelCity(){
        return travelService.findTravelLoad();
    }

    @RequestMapping("/oneload/{id}")
    public String getOneTravelLoad(@PathVariable(value = "id") Integer id,Model model){
        TabTravelWithBLOBs oneTravelLoad = travelService.getOneTravelLoad(id);
        model.addAttribute("travle",oneTravelLoad);
        return "en/travelDetails";
    }

    /**
     * 查询旅游列表
     * @param param
     * @return
     */
    @RequestMapping("/loadstravel")
    @ResponseBody
    public JsonData getTravelLoads(TravelParam param){
        return travelService.getLoadsTravels(param);
    }

    /**
     * 根据时间查询余票
     * @param tabTravelDate
     * @param traNum
     * @return
     */
    @RequestMapping("/loadsdetails")
    @ResponseBody
    public JsonData getOneLoadsDetails(TabTravelDate  tabTravelDate,Integer traNum ){
        JsonData oneLoadsDetails = travelService.getOneLoadsDetails(tabTravelDate, traNum);
        return oneLoadsDetails;
    }

    /**
     *
     * @param tabTravelDate
     * @param traNum
     * @param perNum
     * @return
     */
    @RequestMapping("/loadstickets")
    @ResponseBody
    public JsonData getOneLoadsTickets(TabTravelDate  tabTravelDate,Integer traNum ,
                                       @RequestParam(defaultValue = "1",value = "perNum")Integer perNum){
        JsonData oneLoadsDetails = travelService.getOneLoadsTickets(tabTravelDate, traNum,perNum);
        return oneLoadsDetails;
    }

    /**
     * 判断用户是否登录，如果登录，直接跳到订单页面，如果未登录，则先登录
     * @param travelOrderParam
     * @param model
     * @return
     */
    @RequestMapping("/orderentry")
    public String getOneTravelOrder(HttpServletRequest request,TravelOrderParam travelOrderParam , Model model){
        String str =(String)request.getSession().getAttribute("userType");
        if(str !=null){
            model.addAttribute("param",travelOrderParam);
            return "en/orderEntry";
        }else{
            return  "en/login";
        }
    }

    /**
     * 生成订单
     * @param travelOrder
     * @return
     */
    @RequestMapping("/orderpeos")
    @ResponseBody
    public JsonData getTravelPeos(TabTravelOrder travelOrder){
        JsonData travelPeos = travelService.getTravelPeos(travelOrder);
        return travelPeos;
    }

}
